<?php
class ProducersController extends AppController {

	var $name = 'Producers';

	function index() {
		$user = $this->Session->read('Auth.User');
		if($user['user_role']=='admin'){
			$this->layout = 'admin';
			$this->Producer->recursive = 0;
			$this->set('producers', $this->paginate());
		} else {
			$this->Session->setFlash(__('Bu sayfaya erişmeye yetkiniz yoktur!', true));
			$this->redirect(array('controller'=>'pages','action' => 'index'));
		}
		
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid producer', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->loadModel('Book');
		$this->loadModel('Film');
		$this->loadModel('Music');
		$books = $this->Book->find('all',array('conditions'=>array('producer_id'=>$id),'order'=>'Product.sold_count desc','limit'=>9));
		$films = $this->Film->find('all',array('conditions'=>array('producer_id'=>$id),'order'=>'Product.sold_count desc','limit'=>9));
		$musics = $this->Music->find('all',array('conditions'=>array('producer_id'=>$id),'order'=>'Product.sold_count desc','limit'=>9));
		$producer = $this->Producer->read(null, $id);
		$this->set(compact('producer','books','films','musics'));
	}
	
	function adminview($id = null) {
		$user = $this->Session->read('Auth.User');
		if($user['user_role']=='admin'){
			$this->layout = 'admin';
			if (!$id) {
				$this->Session->setFlash(__('Invalid producer', true));
				$this->redirect(array('action' => 'index'));
			}
			
			$this->set('producer', $this->Producer->read(null, $id));
			
		} else {
			$this->Session->setFlash(__('Bu sayfaya erişmeye yetkiniz yoktur!', true));
			$this->redirect(array('controller'=>'pages','action' => 'index'));
		}
	}

	function add() {
		$user = $this->Session->read('Auth.User');
		if($user['user_role']=='admin'){
			$this->layout = 'admin';
			if (!empty($this->data)) {
				$this->Producer->create();
				if ($this->Producer->save($this->data)) {
					$this->Session->setFlash(__('The producer has been saved', true));
					$this->redirect(array('action' => 'index'));
				} else {
					$this->Session->setFlash(__('The producer could not be saved. Please, try again.', true));
				}
			}
		} else {
			$this->Session->setFlash(__('Bu sayfaya erişmeye yetkiniz yoktur!', true));
			$this->redirect(array('controller'=>'pages','action' => 'index'));
		}
	}

	function edit($id = null) {
		$user = $this->Session->read('Auth.User');
		if($user['user_role']=='admin'){
			$this->layout = 'admin';
			if (!$id && empty($this->data)) {
				$this->Session->setFlash(__('Invalid producer', true));
				$this->redirect(array('action' => 'index'));
			}
			if (!empty($this->data)) {
				if ($this->Producer->save($this->data)) {
					$this->Session->setFlash(__('The producer has been saved', true));
					$this->redirect(array('action' => 'index'));
				} else {
					$this->Session->setFlash(__('The producer could not be saved. Please, try again.', true));
				}
			}
			if (empty($this->data)) {
				$this->data = $this->Producer->read(null, $id);
			}
		} else {
			$this->Session->setFlash(__('Bu sayfaya erişmeye yetkiniz yoktur!', true));
			$this->redirect(array('controller'=>'pages','action' => 'index'));
		}
	}

	function delete($id = null) {
		$user = $this->Session->read('Auth.User');
		if($user['user_role']=='admin'){
			$this->layout = 'admin';
			if (!$id) {
				$this->Session->setFlash(__('Invalid id for producer', true));
				$this->redirect(array('action'=>'index'));
			}
			if ($this->Producer->delete($id)) {
				$this->Session->setFlash(__('Producer deleted', true));
				$this->redirect(array('action'=>'index'));
			}
			$this->Session->setFlash(__('Producer was not deleted', true));
			$this->redirect(array('action' => 'index'));
		} else {
			$this->Session->setFlash(__('Bu sayfaya erişmeye yetkiniz yoktur!', true));
			$this->redirect(array('controller'=>'pages','action' => 'index'));
		}
	}
	
	function beforeFilter() {
	    parent::beforeFilter();
	    $this->Auth->allow(array('*'));
    }
}
